package editor
{
	import Classes.CLoader;
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import cardsbody.pi_pngcard;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class pi_editor extends uif_ecomponent {
		private var _cardBody: pi_cardbody;
		private var _ephoto: pi_photo;
		private var _maskInfo: pi_maskInfo;
		private var _dragPhoto: Boolean;
		private var _startDrag: Point;
		
		public function pi_editor(parent:uif_component, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			var inner: Rectangle = getInner(); 
//			var lw: int = inner.width / 3;
			var space: int 	= 5;
			_ephoto = new pi_photo(this, inner.x + (inner.width - lw) + space, inner.y + 200, lw - space, inner.height - 200, 'tabBlock');
			addEvent(MouseEvent.MOUSE_UP, doCardUp, app);
			addEvent(MouseEvent.MOUSE_MOVE, doCardMove, app);
/*			addEvent(KeyboardEvent.KEY_DOWN, doKeyDown, app.stage);
			addEvent(KeyboardEvent.KEY_UP, doKeyUp, app.stage);*/
		}
		
		public function get lw(): int {
			//return inner.width / 3;
			return 253;
		}
		
/*		protected function doKeyDown(e: KeyboardEvent): void {
			if (e.keyCode == 17) {// Ctrl
				_cardBody.tmplColors.alphaMultiplier = 0.4;
				_cardBody.refresh();
			}
		}
		
		protected function doKeyUp(e: KeyboardEvent): void {
			if (e.keyCode == 17) {// Ctrl
				_cardBody.tmplColors.alphaMultiplier = 1;
				_cardBody.refresh();
			}
		}*/
		
		public function get cardBody(): pi_cardbody {
			return _cardBody;
		}
		
		public function assignPhoto(a_photo: CLoader): void {
			if (_cardBody.photoModified) a_photo.duplicate(_cardBody.card_photo);
			a_photo.duplicate(_ephoto.source);
//			if (_maskInfo.pos) _ephoto.resetSize(new Point(_maskInfo.pos.width, _maskInfo.pos.height));
		}
		
		public function doLoadPhoto(e: Event): void {
			app.dispatchEvent(new ObjectEvent(pi_events.EDITORPHOTOLOADCOMPLETE, _cardBody.card_photo));
		}
		
		public function doCardDown(e: MouseEvent): void {
			if (_cardBody is pi_pngcard) { 
				var pngcard: pi_pngcard = _cardBody as pi_pngcard;
				_startDrag = mousePos;
				if (GONFIG::make) {
					var scale : Number = pngcard.contentSize.x / pngcard.bitmap.bitmapData.width;
					var mat: Matrix = new Matrix();
					mat.translate(-pngcard.bitmap.x, -pngcard.bitmap.y);
					mat.scale(scale, scale);
					var res: Point = mat.transformPoint(mousePos); 
					trace('[' + Math.round(res.x) + ', ' + Math.round(res.y) + '],');
				}
			}
		}
		
		public function doCardUp(e: MouseEvent): void {
			_startDrag = null;
			_ephoto.rect.applySize();
		}
		
		public function photoDrag(offset: Point, a_scale: Number): void {
			_ephoto.rect.offset(offset.x * a_scale, offset.y * a_scale);
		}
		
		protected function reset3DInfo(): void {
			if (!_cardBody.maskInfo.r3d) _cardBody.maskInfo.r3d = [0, 0, 1, 0, 0.5, 0.5];
		}
		
		protected function photoRollZ(offset: Point, a_scale: Number): void {
			_ephoto.rect.rotation = offset.y;
		}
		
/*		protected function photoRoll(offset: Point, a_scale: Number, sIndex: int = 0): void {
			reset3DInfo();
			var v: Point = offset.clone();
			v.normalize(1);
			_cardBody.maskInfo.r3d[sIndex] = v.x;
			_cardBody.maskInfo.r3d[sIndex + 1] = v.y;
			_cardBody.maskInfo.r3d[3] = offset.length;
			_cardBody.refresh();
		}*/
		
		public function doCardMove(e: MouseEvent): void {
			if (_startDrag && (_cardBody.card_photo.loaded) && (_cardBody.card_mask.source.loaded)) {
				var newPos	: Point = mousePos;
				var offset	: Point = _startDrag.subtract(mousePos);
				var scale	: Number = _cardBody.width / _cardBody.card_mask.source.contentLoaderInfo.width *
									_cardBody.width / _cardBody.card_photo.contentLoaderInfo.width * 0.5;
				if (!e.shiftKey && !e.altKey && !e.ctrlKey) {
					photoDrag(offset, scale);
					_startDrag = newPos;
				} //else if (e.altKey) photoRoll(offset, scale, 1);
				else if (e.shiftKey) photoRollZ(offset, scale);
				else if (e.ctrlKey) _ephoto.rect.resizeOffset(offset.y);
			}
		}
		
		public function get photoBlock(): pi_photo {
			return _ephoto;
		}
		
		public function loadMask(a_maskInfo: pi_maskInfo): void {
			var inner: Rectangle = getInner();

			_maskInfo = a_maskInfo;
			if (_cardBody) _cardBody.dispose();
			_cardBody = new _maskInfo.maskClass(this, inner.x, inner.y, inner.width - lw, inner.height);
			_cardBody.addEvent(Event.COMPLETE, doLoadPhoto, _cardBody.card_photo.contentLoaderInfo);
			_cardBody.addEvent(MouseEvent.MOUSE_DOWN, doCardDown);
			
			_cardBody.setMaskInfo(_maskInfo);
			if (_cardBody is pi_pngcard) {
				_cardBody.buttonMode = true;
				_cardBody.mouseChildren = false;
			}
			if (_cardBody.card_photo) 
				_ephoto.source.duplicate(_cardBody.card_photo);
				
			if (GONFIG::make) {
				trace(_maskInfo.id + '\n');
			}
//			if (_maskInfo.pos) _ephoto.resetSize(new Point(_maskInfo.pos.width, _maskInfo.pos.height));
		}
	}
}